<template>
  <div class="dashboard-preview" :style="{ backgroundColor: projectInfo.pageBgColor }">
    <!-- 预览头部 -->
    <div class="preview-header">
      <div class="preview-title">{{ projectInfo.title }}</div>
      <div class="preview-controls">
        <el-button size="small" @click="closePreview">关闭预览</el-button>
        <el-button size="small" @click="refreshPreview">刷新</el-button>
      </div>
    </div>
    
    <!-- 预览画布 -->
    <div class="preview-canvas" :style="{ width: projectInfo.pageWidth + 'px', height: projectInfo.pageHeight + 'px' }">
      <!-- 组件渲染 -->
      <div
        v-for="component in components"
        :key="component.id"
        class="preview-component"
        :class="[`component-${component.type}`, { 'grouped': component.isGrouped }]"
        :style="{
          position: 'absolute',
          left: component.x + 'px',
          top: component.y + 'px',
          width: component.width + 'px',
          height: component.height + 'px',
          backgroundColor: component.bgColor,
          transform: `rotate(${component.rotate || 0}deg)`,
          zIndex: component.isGrouped ? 5 : 1
        }"
      >
        <!-- 组件内容 -->
        <div class="component-content">
          <!-- 柱状图 -->
          <div v-if="component.type === 'bar'" class="chart-container">
            <div class="chart-title">{{ component.title }}</div>
            <div class="bar-chart">
              <div class="bar" style="height: 60%; background: #409EFF;"></div>
              <div class="bar" style="height: 80%; background: #67C23A;"></div>
              <div class="bar" style="height: 45%; background: #E6A23C;"></div>
              <div class="bar" style="height: 90%; background: #F56C6C;"></div>
              <div class="bar" style="height: 70%; background: #909399;"></div>
            </div>
          </div>
          
          <!-- 折线图 -->
          <div v-else-if="component.type === 'line'" class="chart-container">
            <div class="chart-title">{{ component.title }}</div>
            <div class="line-chart">
              <svg width="100%" height="100%" viewBox="0 0 300 150">
                <polyline
                  points="0,120 50,80 100,60 150,40 200,20 250,30 300,10"
                  fill="none"
                  stroke="#409EFF"
                  stroke-width="2"
                />
                <polyline
                  points="0,100 50,70 100,50 150,30 200,15 250,25 300,5"
                  fill="none"
                  stroke="#67C23A"
                  stroke-width="2"
                />
              </svg>
            </div>
          </div>
          
          <!-- 饼图 -->
          <div v-else-if="component.type === 'pie'" class="chart-container">
            <div class="chart-title">{{ component.title }}</div>
            <div class="pie-chart">
              <div class="pie-slice" style="--start: 0deg; --end: 120deg; --color: #409EFF;"></div>
              <div class="pie-slice" style="--start: 120deg; --end: 240deg; --color: #67C23A;"></div>
              <div class="pie-slice" style="--start: 240deg; --end: 360deg; --color: #E6A23C;"></div>
            </div>
          </div>
          
          <!-- 仪表盘 -->
          <div v-else-if="component.type === 'gauge'" class="chart-container">
            <div class="chart-title">{{ component.title }}</div>
            <div class="gauge">
              <div class="gauge-circle">
                <div class="gauge-value">85%</div>
              </div>
            </div>
          </div>
          
          <!-- 统计卡片 -->
          <div v-else-if="component.type === 'stat'" class="stat-container">
            <div class="stat-title">{{ component.title }}</div>
            <div class="stat-value">1,234</div>
            <div class="stat-trend">↑ 12.5%</div>
          </div>
          
          <!-- 数据表格 -->
          <div v-else-if="component.type === 'table'" class="table-container">
            <div class="table-title">{{ component.title }}</div>
            <div class="table-content">
              <div class="table-row header">
                <div class="table-cell">设备名称</div>
                <div class="table-cell">状态</div>
                <div class="table-cell">运行时间</div>
              </div>
              <div class="table-row">
                <div class="table-cell">设备A</div>
                <div class="table-cell"><span class="status normal">正常</span></div>
                <div class="table-cell">24小时</div>
              </div>
              <div class="table-row">
                <div class="table-cell">设备B</div>
                <div class="table-cell"><span class="status warning">警告</span></div>
                <div class="table-cell">18小时</div>
              </div>
            </div>
          </div>
          
          <!-- 进度环 -->
          <div v-else-if="component.type === 'progress'" class="progress-container">
            <div class="progress-title">{{ component.title }}</div>
            <div class="progress-ring">
              <div class="ring" style="--progress: 75%">
                <div class="progress-value">75%</div>
              </div>
            </div>
          </div>
          
          <!-- 默认组件 -->
          <div v-else class="default-component">
            <div class="component-title">{{ component.title }}</div>
            <div class="component-placeholder">
              <i class="el-icon-data-line"></i>
              <span>{{ getComponentTypeName(component.type) }}</span>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 分组边框显示 -->
      <div
        v-for="group in groups"
        :key="`group-${group.id}`"
        class="preview-group"
        :style="{
          position: 'absolute',
          left: group.x + 'px',
          top: group.y + 'px',
          width: group.width + 'px',
          height: group.height + 'px',
          border: '2px dashed rgba(64, 158, 255, 0.6)',
          borderRadius: '4px',
          pointerEvents: 'none',
          zIndex: 4
        }"
      >
        <div class="group-label">{{ group.name }}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { ElMessage } from 'element-plus'

// 预览数据类型
interface PreviewData {
  projectInfo: {
    title: string
    pageWidth: number
    pageHeight: number
    pageBgColor: string
  }
  components: Array<{
    id: number
    type: string
    x: number
    y: number
    width: number
    height: number
    title: string
    bgColor: string
    rotate?: number
    chartType?: string
    showLegend?: boolean
    valueFormat?: string
    groupId?: number
    isGrouped?: boolean
  }>
  groups: Array<{
    id: number
    name: string
    componentIds: number[]
    x: number
    y: number
    width: number
    height: number
  }>
}

// 响应式数据
const previewData = ref<PreviewData | null>(null)

// 计算属性
const projectInfo = computed(() => previewData.value?.projectInfo || {
  title: '预览页面',
  pageWidth: 1920,
  pageHeight: 1080,
  pageBgColor: '#1a2b4c'
})

const components = computed(() => previewData.value?.components || [])
const groups = computed(() => previewData.value?.groups || [])

// 获取组件类型名称
const getComponentTypeName = (type: string) => {
  const typeNames: Record<string, string> = {
    bar: '柱状图',
    line: '折线图',
    pie: '饼图',
    gauge: '仪表盘',
    stat: '统计卡片',
    table: '数据表格',
    progress: '进度环'
  }
  return typeNames[type] || '未知组件'
}

// 加载预览数据
const loadPreviewData = () => {
  try {
    const data = localStorage.getItem('dashboardPreviewData')
    if (data) {
      previewData.value = JSON.parse(data)
      ElMessage.success('预览数据加载成功')
    } else {
      ElMessage.warning('未找到预览数据')
    }
  } catch (error) {
    console.error('加载预览数据失败:', error)
    ElMessage.error('加载预览数据失败')
  }
}

// 关闭预览
const closePreview = () => {
  window.close()
}

// 刷新预览
const refreshPreview = () => {
  loadPreviewData()
}

// 组件挂载时加载数据
onMounted(() => {
  loadPreviewData()
})
</script>

<style lang="scss" scoped>
.dashboard-preview {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  position: relative;
  
  .preview-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 50px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    z-index: 1000;
    
    .preview-title {
      font-size: 18px;
      font-weight: bold;
    }
    
    .preview-controls {
      display: flex;
      gap: 10px;
    }
  }
  
  .preview-canvas {
    position: relative;
    margin: 60px auto 0;
    background: transparent;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
  }
  
  .preview-component {
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    
    &.grouped {
      border: 1px solid rgba(64, 158, 255, 0.3);
    }
    
    .component-content {
      width: 100%;
      height: 100%;
      padding: 10px;
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
    }
  }
  
  // 图表组件样式
  .chart-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    
    .chart-title {
      font-size: 14px;
      font-weight: bold;
      margin-bottom: 10px;
      text-align: center;
      color: #333;
    }
  }
  
  .bar-chart {
    flex: 1;
    display: flex;
    align-items: flex-end;
    justify-content: space-around;
    padding: 0 10px;
    
    .bar {
      width: 20%;
      border-radius: 3px 3px 0 0;
      transition: all 0.3s;
      
      &:hover {
        opacity: 0.8;
      }
    }
  }
  
  .line-chart {
    flex: 1;
    position: relative;
  }
  
  .pie-chart {
    flex: 1;
    position: relative;
    width: 80px;
    height: 80px;
    margin: 0 auto;
    
    .pie-slice {
      position: absolute;
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background: conic-gradient(
        var(--color) var(--start) var(--end),
        transparent var(--end)
      );
    }
  }
  
  .gauge {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    
    .gauge-circle {
      width: 80px;
      height: 40px;
      background: conic-gradient(#67C23A 0deg 306deg, #E6E6E6 306deg 360deg);
      border-radius: 80px 80px 0 0;
      position: relative;
      display: flex;
      align-items: center;
      justify-content: center;
      
      .gauge-value {
        font-size: 16px;
        font-weight: bold;
        color: #333;
      }
    }
  }
  
  // 统计卡片样式
  .stat-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    
    .stat-title {
      font-size: 14px;
      color: #666;
      margin-bottom: 10px;
    }
    
    .stat-value {
      font-size: 32px;
      font-weight: bold;
      color: #409EFF;
      margin-bottom: 5px;
    }
    
    .stat-trend {
      font-size: 12px;
      color: #67C23A;
    }
  }
  
  // 表格样式
  .table-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    
    .table-title {
      font-size: 14px;
      font-weight: bold;
      margin-bottom: 10px;
      text-align: center;
      color: #333;
    }
    
    .table-content {
      flex: 1;
      font-size: 12px;
      
      .table-row {
        display: flex;
        border-bottom: 1px solid #eee;
        
        &.header {
          font-weight: bold;
          background: #f5f7fa;
        }
        
        .table-cell {
          flex: 1;
          padding: 5px 8px;
          
          .status {
            padding: 2px 6px;
            border-radius: 3px;
            font-size: 10px;
            
            &.normal {
              background: #e1f3d8;
              color: #67C23A;
            }
            
            &.warning {
              background: #faecd8;
              color: #E6A23C;
            }
          }
        }
      }
    }
  }
  
  // 进度环样式
  .progress-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    
    .progress-title {
      font-size: 14px;
      margin-bottom: 10px;
      text-align: center;
      color: #333;
    }
    
    .progress-ring {
      .ring {
        width: 70px;
        height: 70px;
        border-radius: 50%;
        background: conic-gradient(#409EFF 0deg calc(var(--progress) * 3.6deg), #eee calc(var(--progress) * 3.6deg) 360deg);
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        
        &::before {
          content: "";
          position: absolute;
          width: 50px;
          height: 50px;
          background: white;
          border-radius: 50%;
        }
        
        .progress-value {
          position: relative;
          z-index: 1;
          font-size: 16px;
          font-weight: bold;
          color: #333;
        }
      }
    }
  }
  
  // 默认组件样式
  .default-component {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    
    .component-title {
      font-size: 14px;
      font-weight: bold;
      margin-bottom: 10px;
      color: #333;
    }
    
    .component-placeholder {
      display: flex;
      flex-direction: column;
      align-items: center;
      color: #999;
      
      i {
        font-size: 24px;
        margin-bottom: 5px;
      }
      
      span {
        font-size: 12px;
      }
    }
  }
  
  // 分组样式
  .preview-group {
    .group-label {
      position: absolute;
      top: -20px;
      left: 0;
      background: rgba(64, 158, 255, 0.8);
      color: white;
      padding: 2px 6px;
      border-radius: 2px;
      font-size: 12px;
    }
  }
}
</style> 